Book Contents

Operator precedence in expressions

The rules that determine the order for evaluating an expression with more than one operator are

  • Operators of equal precedence are evaluated from left to right.
  • Operators of unequal precedence are evaluated according to precedence, from highest to lowest.
  • An operator in a statement enclosed in parentheses takes precedence over operators that are not.

    Precedence Level

    Name

    Symbols

    1 (Highest)

    Parentheses

    ( )

    2

    logical negation

    bitwise complement

    NOT

    ~

    3

    Multiplication

    division

    modulus (remainder)

    *

    /

    MOD, %

     

    exponent

    logical and

    bitwise and

    bitwise right shift

    bitwise left shift

    **

    AND, &&

    &

    >>

    <<

    4

    Addition

    subtraction

    logical or

    bitwise inclusive or

    bitwise exclusive or

    the math functions

    +

    -

    OR, ||

    |

    ^

    sin, etc.

    5 (lowest)

    Equal

    not equal

    less than

    greater than

    less than or equal to

    greater than or equal to

    EQ, ==

    NE, <>

    LT, <

    GT, >

    LE, <=

    GE, >=

See also

About expressions

Example: Operator precedence